var personaInfoEmailFlag = true;
var personaInfoNameFlag = true;
var personaInfoQqFlag = true;
var personaInfoMobileFlag = true;

function showOnePersonaInfoDiv(){
	$("#one_personainfo_div").show();
	$("#two_personainfo_div").hide();
	$("#three_personainfo_div").hide();
	$("#onedivnoticeinfo").hide();
	$("#twodivnoticeinfo").hide();
	$("#threedivnoticeinfo").hide();
	$("#onemenu").attr("class", "tabsSelected");
	$("#twomenu").attr("class", "tabsUnselected");
	$("#threemenu").attr("class", "tabsUnselected");
}

function showTwoPersonaInfoDiv(){
	$("#one_personainfo_div").hide();
	$("#three_personainfo_div").hide();
	$("#two_personainfo_div").show();
	$("#onedivnoticeinfo").hide();
	$("#twodivnoticeinfo").hide();
	$("#threedivnoticeinfo").hide();
	$("#onemenu").attr("class", "tabsUnselected");
	$("#threemenu").attr("class", "tabsUnselected");
	$("#twomenu").attr("class", "tabsSelected");
}

function showThreePersonaInfoDiv(){
	$("#one_personainfo_div").hide();
	$("#two_personainfo_div").hide();
	$("#three_personainfo_div").show();
	$("#onedivnoticeinfo").hide();
	$("#twodivnoticeinfo").hide();
	$("#threedivnoticeinfo").hide();
	$("#onemenu").attr("class", "tabsUnselected");
	$("#twomenu").attr("class", "tabsUnselected");
	$("#threemenu").attr("class", "tabsSelected");
}

function resetPersonainfoForm(){
	document.personainfoform.reset();
}

function submitPersonainfoForm(nameUrl,emailUrl,url){
	validateQq();
	validateMobile();
	validateName(nameUrl);
	validateEmail(emailUrl);
	
	if(personaInfoQqFlag == true && personaInfoMobileFlag == true && personaInfoEmailFlag == true && personaInfoNameFlag == true){
		$.getJSON(url, {
			nickName : encodeURI($("#nickName").val()),
			name : encodeURI($("#name").val()),
			qq : $("#qq").val(),
			email : encodeURI($("#email").val()),
			mobile : $("#mobile").val()
		}, function(action) {
			if (action.flag == "TIMEOUT") {
				alert("你登陆时间过长，请重新登录.");
				var loginUrl = url.replace("service/updateUserInfo.json","login");
				window.location.href = loginUrl;
			}else if(action.flag == "ERROR"){
				alert("服务器错误,请稍后再试.");
			}else {
				alert("修改用户基本信息成功!");
			}
		});
	}
}

function getfocus(info){
	$("#" + info).show();
	
	if("nameInfo" == info){
		$("#nameError").hide();
		$("#nameLenError").hide();
		$("#nameExistError").hide();
	}
	if("qqInfo" == info){
		$("#qqError").hide();
	}
	if("emailInfo" == info){
		$("#emailNullError").hide();
		$("#emailError").hide();
		$("#emailExistError").hide();
	}
	if("mobileInfo" == info){
		$("#mobileError").hide();
	}
	
}

function loseblur(info){
	$("#" + info).hide();
	
	if("qqInfo" == info){
		validateQq();
	}
	
	if("mobileInfo" == info){
		validateMobile();
	}
}

function loseblurEmail(url){
	$("#emailInfo").hide();
	validateEmail(url);
}

function loseblurName(url){
	$("#nameInfo").hide();
	validateName(url);
}

function validateEamilExist(email,url){
	$.ajax({url: url, data:{email:email},async: false, success: function(data) {
		if (data.flag == "TIMEOUT") {
			alert("你登陆时间过长，请重新登录.");
			var loginUrl = url.replace("service/validateEmail.json","login");
			window.location.href = loginUrl;
		}else if(data.flag == "VALIDATE_EXIST_KEY"){
			$("#emailExistError").show();
			 personaInfoEmailFlag =  false;
		 }else{
			 $("#emailExistError").hide();
			 personaInfoEmailFlag = true;
		 }  
    }});
	
}

function validateNameExist(name,url){	
	$.ajax({url: url, data:{name:name},async: false, success: function(data) {
		if (data.flag == "TIMEOUT") {
			alert("你登陆时间过长，请重新登录.");
			var loginUrl = url.replace("service/validateName.json","login");
			window.location.href = loginUrl;
		}else if(data.flag == "VALIDATE_EXIST_KEY"){
			$("#nameExistError").show();
			personaInfoNameFlag =  false;
		 }else{
			 $("#nameExistError").hide();
			 personaInfoNameFlag = true;
		 }  
    }});
}

function validateName(url){
	$("#nameError").hide();
	$("#nameLenError").hide();
	$("#nameExistError").hide();
	
	if($("#name").val() == ""){
		$("#nameError").show();
		personaInfoNameFlag = false;
		return;
	}else if($("#name").val().length > 45){
		$("#nameLenError").show();
		personaInfoNameFlag = false;
		return;
	}else{
		personaInfoNameFlag = true;
	}
	
	validateNameExist($("#name").val(),url);
}

function validateEmail(url){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	$("#emailNullError").hide();
	$("#emailError").hide();
	$("#emailExistError").hide();
	
	if($("#email").val() == ""){
		$("#emailNullError").show();
		personaInfoEmailFlag = false;
		return;
	}else if(!filter.test($("#email").val())){
	   $("#emailError").show();
	   personaInfoEmailFlag = false;
	   return;
	}else{
		personaInfoEmailFlag = true;
	}
	
	validateEamilExist($("#email").val(),url);
}

function validateQq(){
	var reg = /^[1-9]\d{4,8}$/;
	
	if($("#qq").val() != ""){
		if(reg.test($("#qq").val())){
			personaInfoQqFlag = true;
			$("#qqError").hide();
		}else{
			$("#qqError").show();
			personaInfoQqFlag = false;
		}
	}else{
		personaInfoQqFlag = true;
		$("#qqError").hide();
	}
}

function validateMobile(){
	var re= /^(1)\d{10}$/;
	
	if($("#mobile").val() != ""){
	  if(re.test($("#mobile").val())){
		personaInfoMobileFlag = true;
		$("#mobileError").hide();
	  }else{
		$("#mobileError").show();
		personaInfoMobileFlag = false;
      }
	}else{
    	personaInfoMobileFlag = true;
		$("#mobileError").hide();
	}
}

//update paassword
var newPasswordFlag = true;
var confirmPasswordFlag = true;
var passwordFlag = true;

function getFocusPassword(info){
	$("#" + info).show();
	
	if("passwordinfo" == info){
		$("#passworderror").hide();
	}
	if("newpasswordinfo" == info){
		$("#newpassworderror").hide();
	}
	if("confirmpasswordinfo" == info){
		$("#confirmpassworderror").hide();
	}
}

function loseBlurPassword(info){
	$("#" + info).hide();
	
	if("newpasswordinfo" == info){
		validateNewPassword();
	}
	if("confirmpasswordinfo" == info){
		validateConfirmPassword();
	}
}

function validatePassword(url){
	$("#passwordinfo").hide();
	
	$.ajax({url: url, data:{password:$("#password").val()},async: false, success: function(data) {
		if (data.flag == "TIMEOUT") {
			alert("你登陆时间过长，请重新登录.");
			var loginUrl = url.replace("service/validatePassword.json","login");
			window.location.href = loginUrl;
		}else if(data.flag == "VALIDATE_RIGHT_KEY"){
			$("#passworderror").hide();
			passwordFlag = true;
		 }else{
			$("#passworderror").show();
			passwordFlag =  false;
		 }  
    }});
}

function validateNewPassword(){
	if($("#newpassword").val().length < 6 || $("#newpassword").val().length > 15){
		newPasswordFlag = false;
		$("#newpassworderror").show();
	}else{
		newPasswordFlag = true;
	}
}

function validateConfirmPassword(){
	if($("#newpassword").val() != $("#confirmpassword").val()){
		confirmPasswordFlag = false;
		$("#confirmpassworderror").show();
	}else{
		confirmPasswordFlag = true;
	}
}

function resetUpdatePasswordForm(){
	document.personapasswordform.reset();
}

function submitUpdatePsswordForm(url,updateUrl){
	validatePassword(url);
	validateNewPassword();
	validateConfirmPassword();
	
	if(newPasswordFlag == true && confirmPasswordFlag == true && passwordFlag == true){
		$.getJSON(updateUrl, {
			newpassword : $("#newpassword").val()
		}, function(action) {
			if (action.flag == "TIMEOUT") {
				alert("你登陆时间过长，请重新登录.");
				var loginUrl = updateUrl.replace("service/updatePassword.json","login");
				window.location.href = loginUrl;
			}else if(action.flag == "ERROR"){
				alert("服务器错误,请稍后再试.");
			}else {
				alert("修改密码成功!");
				document.personapasswordform.reset();
			}
		});
	}
}

//修改扩展信息
var extendBirthdayFlag = true;
var extendDispostionFlag = true;

function getFocusExtend(info){
$("#" + info).show();
	
	if("dispostioninfo" == info){
		$("#dispostionerror").hide();
	}
}

function loseBlurExtend(info){
	$("#" + info).hide();
	
	if("dispostioninfo" == info){
		validateDispostion();
	}
}

function loseBlurExtendBirthday(){
	if($("#birthday").val() == ""){
		$("#birthdayerror").show();
		extendBirthdayFlag = false;
	}else{
		$("#birthdayerror").hide();
		extendBirthdayFlag = true;
	}
}

function validateDispostion(){
	if($("#disposition").val().length > 25){
		$("#dispostionerror").show();
		extendDispostionFlag = false;
	}else{
		$("#dispostionerror").hide();
		extendDispostionFlag = true;
	}
}

function resetUpdateExtendForm(){
	document.personaextendform.reset();
}

function submitUpdateExtendForm(url){
	loseBlurExtendBirthday();
	validateDispostion();
	var sexTemp;
	var parentSexTemp;
	
	if(extendBirthdayFlag == true && extendDispostionFlag == true){
		for ( var i = 0; i < document.personaextendform.sexTemp.length; i++) {
			if (document.personaextendform.sexTemp[i].checked) {
				sexTemp = document.personaextendform.sexTemp[i].value;
			}
		}
		
		for ( var i = 0; i < document.personaextendform.parentSexTemp.length; i++) {
			if (document.personaextendform.parentSexTemp[i].checked) {
				parentSexTemp = document.personaextendform.parentSexTemp[i].value;
			}
		}
		
		$.getJSON(url, {
			sexTemp : sexTemp,
			parentSexTemp :parentSexTemp,
			birthday : $("#birthday").val(),
			disposition : $("#disposition").val()
		}, function(action) {
			if (action.flag == "TIMEOUT") {
				alert("你登陆时间过长，请重新登录.");
				var loginUrl = url.replace("service/updateExtend.json","login");
				window.location.href = loginUrl;
			}else if(action.flag == "ERROR"){
				alert("服务器错误,请稍后再试.");
			}else {
				alert("修改信息成功!");
			}
		});
	}
}

function activeEmail(url){

	if($("#activePassword").val() == ""){
		$("#activepasswordError").show();
		return;
	}
	
	$.ajax({url: url, data:{password:$("#activePassword").val(),uuid:$("#activeUUID").val(),userId:$("#activeUserId").val(),email:$("#activeEmail").val()},async: false, success: function(data) {
		if(data.flag == "ERROR"){
			alert("激活邮箱失败,请检查您输入密码!");
		 }else{
			 alert("激活新邮箱成功，即将转入登录界面!");
			 var loginUrl = url.replace("active/doActiveNewEmail.json","login");
			 window.location.href = loginUrl;
		 }  
    }});
}